#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;



//method 1
int a[8];
int b[2*8-1];
int c[2*8-1];

void getEightQueens(int level, vector<int> &pos, int &result)
{
    if (level == 8) {
        result++;
        return;
    }

   for (int i = 0; i < 8; i++) {
      if (a[i] || b[level+i] || c[level-i+7]) continue;
      a[i] = 1;
      b[level+i] = 1;
      c[level-i+7] = 1;
      pos[level] = i;
      getEightQueens(level+1, pos, result);
      a[i] = 0;
      b[level+i] = 0;
      c[level-i+7] = 0;
   }

}


//method 2
bool valid(int i, int level, vector<int> &pos)
{
    for (int j = 0; j < level; j++)
        if (pos[j] == i || abs(j-level) == abs(pos[j] - i)) {
            return false;
        }
    return true;
}

void getEightQueens2(int level, vector<int> &pos, int &result)
{
    if (level == 8) {
        result++;
        return;
    }

   for (int i = 0; i < 8; i++) {
       if (!valid(i, level, pos)) continue;
       pos[level] = i;
       getEightQueens2(level+1, pos, result);
   }

}

int main(int argc, char **argv)
{
    vector<int> pos(8, 0);
    int result = 0;
    getEightQueens(0, pos, result);
    cout << result << endl;

    result = 0;
    vector<int> pos2(8, 0);
    getEightQueens2(0, pos2, result);
    cout << result << endl;
}
